package com.smart.community.region.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 区域统计视图对象
 * 
 * @author Wu.Liang
 * @since 2024-12-21
 */
@Data
@Schema(description = "区域统计视图对象")
public class RegionStatsVO implements Serializable {

    private static final long serialVersionUID = 1L;

    @Schema(description = "统计ID")
    private Long id;

    @Schema(description = "区域ID")
    private Long regionId;

    @Schema(description = "区域名称")
    private String regionName;

    @Schema(description = "统计日期")
    private LocalDate statDate;

    @Schema(description = "社区数量")
    private Integer communityCount;

    @Schema(description = "楼栋数量")
    private Integer buildingCount;

    @Schema(description = "房户数量")
    private Integer householdCount;

    @Schema(description = "居民数量")
    private Integer residentCount;

    @Schema(description = "总面积（平方米）")
    private BigDecimal areaTotal;

    @Schema(description = "入住率（%）")
    private BigDecimal occupancyRate;

    @Schema(description = "户均面积（平方米）")
    private BigDecimal averageAreaPerHousehold;

    @Schema(description = "人均面积（平方米）")
    private BigDecimal averageAreaPerPerson;

    @Schema(description = "户均人数")
    private BigDecimal averagePersonPerHousehold;

    @Schema(description = "楼栋密度（每平方公里楼栋数）")
    private BigDecimal buildingDensity;

    @Schema(description = "创建时间")
    private LocalDateTime createTime;

    @Schema(description = "更新时间")
    private LocalDateTime updateTime;

    // ==================== 业务方法 ====================

    /**
     * 计算户均面积
     */
    public BigDecimal getAverageAreaPerHousehold() {
        if (areaTotal == null) {
            return BigDecimal.ZERO;
        }
        if (householdCount == null || householdCount == 0) {
            return BigDecimal.ZERO;
        }
        return areaTotal.divide(BigDecimal.valueOf(householdCount), 2, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 计算人均面积
     */
    public BigDecimal getAverageAreaPerPerson() {
        if (areaTotal == null) {
            return BigDecimal.ZERO;
        }
        if (residentCount == null || residentCount == 0) {
            return BigDecimal.ZERO;
        }
        return areaTotal.divide(BigDecimal.valueOf(residentCount), 2, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 计算户均人数
     */
    public BigDecimal getAveragePersonPerHousehold() {
        if (residentCount == null) {
            return BigDecimal.ZERO;
        }
        if (householdCount == null || householdCount == 0) {
            return BigDecimal.ZERO;
        }
        return BigDecimal.valueOf(residentCount)
                .divide(BigDecimal.valueOf(householdCount), 2, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 计算楼栋密度
     */
    public BigDecimal getBuildingDensity() {
        if (buildingCount == null) {
            return BigDecimal.ZERO;
        }
        if (areaTotal == null || areaTotal.compareTo(BigDecimal.ZERO) == 0) {
            return BigDecimal.ZERO;
        }
        // 每平方公里楼栋数
        return BigDecimal.valueOf(buildingCount)
                .divide(areaTotal.divide(BigDecimal.valueOf(1000000), 6, BigDecimal.ROUND_HALF_UP), 2, BigDecimal.ROUND_HALF_UP);
    }
} 